﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using System.Threading;
using WF = System.Windows.Forms;

namespace JustPlus
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static WF.NotifyIcon notifyIcon = null;
        private MainWindow mwnd ;
        public MainWindow()
        {
            InitializeComponent();
            this.Width = 512;
            this.Height = 256;
            LoadPlayerPanel();
            LoadTray();
        }

        //加载主界面
        private void LoadPlayerPanel()
        {
            OperationPanel opearionpanel = new OperationPanel();
            stackPanel2.Children.Add(opearionpanel);
            PlayerPanel playpanel = new PlayerPanel();
            stackPanel3.Children.Add(playpanel);

            mwnd = (MainWindow)Application.Current.MainWindow;
        }

        //加载系统托盘
        private void LoadTray()
        {
            //隐藏主窗体
            //this.Visibility = Visibility.Hidden;

            //设置托盘的各个属性
            notifyIcon = new WF.NotifyIcon();
            notifyIcon.Text = "豆瓣FM，好音乐与您不期而遇！";

            System.Drawing.Icon icon = new System.Drawing.Icon(@"logo.ico");
            notifyIcon.Icon = icon;
            notifyIcon.Visible = true;

            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);

            WF.MenuItem[] menuitems = new WF.MenuItem[11];
            menuitems[0] = new WF.MenuItem("打开主界面",new EventHandler(open_MainPanel));
            menuitems[1] = new WF.MenuItem("-");
            menuitems[2] = new WF.MenuItem("播放/暂停",new EventHandler(playOrPause));
            menuitems[3] = new WF.MenuItem("跳过",new EventHandler(next));
            menuitems[4] = new WF.MenuItem("喜欢/取消喜欢", new EventHandler(likeOrUnlike));
            menuitems[5] = new WF.MenuItem("不再播放", new EventHandler(never));
            menuitems[6] = new WF.MenuItem("-");
            menuitems[7] = new WF.MenuItem("设置", new EventHandler(setting));
            menuitems[8] = new WF.MenuItem("关于", new EventHandler(about));
            menuitems[9] = new WF.MenuItem("更新", new EventHandler(update));
            menuitems[10] = new WF.MenuItem("退出", new EventHandler(exit));

            WF.ContextMenu childen = new WF.ContextMenu(menuitems);
            notifyIcon.ContextMenu = childen;

            //窗体状态改变时候触发
            this.StateChanged += new EventHandler(SysTray_StateChanged);
        }

        private void open_MainPanel(object sender, EventArgs e)
        {
            this.ShowInTaskbar = true;
            this.WindowState = WindowState.Normal;
            this.Activate();
        }

        private void playOrPause(object sender, EventArgs e)
        {
            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            playerpanel.image4_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
        }

        private void next(object sender, EventArgs e)
        {
            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            playerpanel.image5_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
        }

        private void likeOrUnlike(object sender, EventArgs e)
        {
            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            playerpanel.image6_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
        }

        private void never(object sender, EventArgs e)
        {
            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            playerpanel.image7_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
        }

        private void setting(object sender, EventArgs e)
        {
            if (mwnd.stackPanel1.Children.Count == 1 && mwnd.Width < 400)
                return;
            this.WindowState = WindowState.Normal;
            OperationPanel operationpanel = (OperationPanel)mwnd.stackPanel2.Children[0];
            operationpanel.image5_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
        }

        private void about(object sender, EventArgs e)
        {
            if (mwnd.stackPanel1.Children.Count == 1 && mwnd.Width < 400)
                return;
            this.WindowState = WindowState.Normal;
            OperationPanel operationpanel = (OperationPanel)mwnd.stackPanel2.Children[0];
            operationpanel.image8_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
        }

        private void update(object sender, EventArgs e)
        {
            if (mwnd.stackPanel1.Children.Count == 1 && mwnd.Width < 400)
                return;
        }

        private void exit(object sender, EventArgs e)
        {
            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            playerpanel.image3_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
        }

        private void notifyIcon_MouseClick(object sender, WF.MouseEventArgs e)
        {
            //如果鼠标左键单击
            if (e.Button == WF.MouseButtons.Left)
            {
                this.ShowInTaskbar = true;
                this.WindowState = WindowState.Normal;
                this.Activate();
            }
        }

        private void SysTray_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.ShowInTaskbar = false;
            }
        }

        
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

        //任务栏按钮事件
        private void PlayThumbNail_Click(object sender, EventArgs e)
        {
            if (PlayThumbNail.Description == @"暂停")
            {
                PlayThumbNail.ImageSource = (ImageSource)Resources["PlayThumbNailImage"];
                PlayThumbNail.Description = @"播放";
            }
            else if (PlayThumbNail.Description == @"播放")
            {
                PlayThumbNail.ImageSource = (ImageSource)Resources["PauseThumbNailImage"];
                PlayThumbNail.Description = @"暂停";
            }

            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            playerpanel.image4_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
        }

        private void NextThumbNail_Click(object sender, EventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            playerpanel.image5_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
        }

        private void LikeThumbNail_Click(object sender, EventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            this.IsEnabled = playerpanel.image6.IsEnabled;
            if (this.IsEnabled)
            {
                if (LikeThumbNail.Description == @"喜欢")
                {
                    LikeThumbNail.ImageSource = (ImageSource)Resources["LikeThumbNailImage"];
                    LikeThumbNail.Description = @"取消喜欢";
                }
                else if (LikeThumbNail.Description == @"取消喜欢")
                {
                    LikeThumbNail.ImageSource = (ImageSource)Resources["FavThumbNailImage"];
                    LikeThumbNail.Description = @"喜欢";
                }
                playerpanel.image6_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
            }
        }

        private void UnlikeThumbNail_Click(object sender, EventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            //this.IsEnabled = playerpanel.image7.IsEnabled;
            if (this.IsEnabled)
            {
                playerpanel.image7_MouseLeftButtonDown(sender, e as MouseButtonEventArgs);
            }
        }

        //防止托盘图标资源未释放
        private void Window_Closed(object sender, EventArgs e)
        {
            if (notifyIcon != null)
                notifyIcon.Dispose();
        }
    }
}
